package io.dy.config;

import io.dy.bean.Bean_01;
import io.dy.bean.Bean_02;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.stream.Stream;

/**
 * @author : dmj
 * @date: 2024/6/3 6:53 下午
 * @description: 演示获取 BeanDefinition 源头
 */
//@Configuration
public class Dy_01 {

    public static void main(String[] args) {
        //1、创建 SpringApplication
        SpringApplication application = new SpringApplication();

        //2、添加源信息
        application.addPrimarySources(Collections.singletonList(Dy_01.class));

        //3、添加其他源信息 比如 xml
        HashSet<String> set = new HashSet<>();
        set.add("classpath:dy_01.xml");
        application.setSources(set);

        //3、run容器
        ConfigurableApplicationContext context = application.run(args);

        //4、查看BeanDefinition 以及 来源
        for (String name : context.getBeanDefinitionNames()) {
            System.out.println("\t 名称:" + name + " 来源:" + context.getBeanFactory().getBeanDefinition(name).getResourceDescription());
        }
    }


    @Bean
    public Bean_01 bean_01(){
        return new Bean_01();
    }

    @Bean
    public Bean_02 bean_02(){
        return new Bean_02();
    }

    @Bean
    public TomcatServletWebServerFactory servletWebServerFactory(){
        return new TomcatServletWebServerFactory();
    }
}
